﻿using FarPoint.Win.Spread;
using Spread.Common.Features.Sample;
using Spread.Worksheet.Properties;
using System.IO;
using System.Windows.Forms;

namespace Spread.Worksheet.Features
{
    public partial class ProtectionDialog : BaseSampleControl
    {
        private bool _isEmptyPasswordProtected;

        public ProtectionDialog()
        {
            InitializeComponent();
            Name = "Protection Dialog";
            SetupSpread();
        }

        private void SetupSpread()
        {
            _fpSpread.Features.EnhancedShapeEngine = true;

            using (var stream = new MemoryStream(Resources.SalesOrders))
            {
                _fpSpread.OpenExcel(stream);
            }

            _fpSpread.ActiveSheet.PropertyChanged += OnPropertyChanged;
            _fpSpread.Features.AutoCreateHyperlink = true;
            _fpSpread.Features.ExcelCompatibleKeyboardShortcuts = true;
            _fpSpread.TabStrip.Editable = true;
            _isEmptyPasswordProtected = false;
        }

        private void OnProtectSheetMenuItemClick(object sender, System.EventArgs e)
        {
            if (_fpSpread.ActiveSheet == null)
                return;

            if (!_isEmptyPasswordProtected)
            {
                FarPoint.Win.Spread.Dialogs.ProtectSheetDialog protectSheetDialog = new FarPoint.Win.Spread.Dialogs.ProtectSheetDialog(_fpSpread);
                protectSheetDialog.ShowDialog(this);
            }
            else
            {
                _fpSpread.AsWorkbook().ActiveSheet.Unprotect(string.Empty);
            }
        }

        private void OnProtectTabStripMenuItemClick(object sender, System.EventArgs e)
        {
            _fpSpread.TabStrip.Editable = !_fpSpread.TabStrip.Editable;

            if (!_fpSpread.TabStrip.Editable)
            {
                _fpSpread.Focus();
                _protectTabStripMenuItem.Text = "Un&protect TabStrip";
            }
            else
            {
                _protectTabStripMenuItem.Text = "&Protect TabStrip";
            }
        }

        private void OnFormatCellsMenuItemClick(object sender, System.EventArgs e)
        {
            ShowFormatCellsDialog();
        }

        private void ShowFormatCellsDialog()
        {
            if (_fpSpread.ActiveSheet == null)
                return;

            var workSheet = _fpSpread.ActiveSheet.AsWorksheet();

            if (!_fpSpread.ActiveSheet.Protect || (_fpSpread.ActiveSheet.Protect && workSheet.Protection.AllowFormattingCells))
            {
                var dialog = FarPoint.Win.Spread.Dialogs.BuiltInDialogs.FormatCells(workSheet.Selection);
                dialog.ShowDialog();
            }
        }

        private void OnPropertyChanged(object sender, SheetViewPropertyChangeEventArgs e)
        {
            _isEmptyPasswordProtected = false;

            if (_fpSpread.ActiveSheet.AsWorksheet().ProtectionMode == GrapeCity.Spreadsheet.ProtectionMode.Protected)
            {
                _isEmptyPasswordProtected = true;
            }

            if (_fpSpread.ActiveSheet.Protect)
                _protectSheetMenuItem.Text = "Un&protect Sheet";
            else
                _protectSheetMenuItem.Text = "&Protect Sheet";
        }
    }
}

//
// ProtectionDialog.Designer.cs
// 

namespace Spread.Worksheet.Features
{
    partial class ProtectionDialog
    {
        /// <summary> 
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary> 
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Component Designer generated code

        /// <summary> 
        /// Required method for Designer support - do not modify 
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.components = new System.ComponentModel.Container();
            System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(ProtectionDialog));
            this.contextMenuStrip1 = new System.Windows.Forms.ContextMenuStrip(this.components);
            this.menuStrip1 = new System.Windows.Forms.MenuStrip();
            this._protectSheetMenuItem = new System.Windows.Forms.ToolStripMenuItem();
            this._protectTabStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
            this._formatCellsMenuItem = new System.Windows.Forms.ToolStripMenuItem();
            this.splitContainer1 = new System.Windows.Forms.SplitContainer();
            this.splitContainer2 = new System.Windows.Forms.SplitContainer();
            this.nameBox1 = new FarPoint.Win.Spread.NameBox();
            this._fpSpread = new FarPoint.Win.Spread.FpSpread(FarPoint.Win.Spread.LegacyBehaviors.None, ((object)(resources.GetObject("splitContainer2.Panel1.Controls"))));
            this._fpSpread_Sheet1 = this._fpSpread.GetSheet(0);
            this.panel1 = new System.Windows.Forms.Panel();
            this.formulaTextBox1 = new FarPoint.Win.Spread.FormulaTextBox();
            this.pnlMain.SuspendLayout();
            this.menuStrip1.SuspendLayout();
            ((System.ComponentModel.ISupportInitialize)(this.splitContainer1)).BeginInit();
            this.splitContainer1.Panel1.SuspendLayout();
            this.splitContainer1.Panel2.SuspendLayout();
            this.splitContainer1.SuspendLayout();
            ((System.ComponentModel.ISupportInitialize)(this.splitContainer2)).BeginInit();
            this.splitContainer2.Panel1.SuspendLayout();
            this.splitContainer2.Panel2.SuspendLayout();
            this.splitContainer2.SuspendLayout();
            ((System.ComponentModel.ISupportInitialize)(this._fpSpread)).BeginInit();
            this.panel1.SuspendLayout();
            this.SuspendLayout();
            // 
            // pnlMain
            // 
            this.pnlMain.Controls.Add(this.splitContainer1);
            this.pnlMain.Controls.Add(this.menuStrip1);
            // 
            // pnlControls
            // 
            this.pnlControls.Size = new System.Drawing.Size(210, 599);
            // 
            // contextMenuStrip1
            // 
            this.contextMenuStrip1.ImageScalingSize = new System.Drawing.Size(20, 20);
            this.contextMenuStrip1.Name = "contextMenuStrip1";
            this.contextMenuStrip1.Size = new System.Drawing.Size(61, 4);
            // 
            // menuStrip1
            // 
            this.menuStrip1.ImageScalingSize = new System.Drawing.Size(20, 20);
            this.menuStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
            this._protectSheetMenuItem,
            this._protectTabStripMenuItem,
            this._formatCellsMenuItem});
            this.menuStrip1.Location = new System.Drawing.Point(0, 0);
            this.menuStrip1.Name = "menuStrip1";
            this.menuStrip1.Size = new System.Drawing.Size(636, 28);
            this.menuStrip1.TabIndex = 0;
            this.menuStrip1.Text = "menuStrip1";
            // 
            // _protectSheetMenuItem
            // 
            this._protectSheetMenuItem.Name = "_protectSheetMenuItem";
            this._protectSheetMenuItem.Size = new System.Drawing.Size(111, 24);
            this._protectSheetMenuItem.Text = "&Protect Sheet";
            this._protectSheetMenuItem.Click += new System.EventHandler(this.OnProtectSheetMenuItemClick);
            // 
            // _protectTabStripMenuItem
            // 
            this._protectTabStripMenuItem.Name = "_protectTabStripMenuItem";
            this._protectTabStripMenuItem.Size = new System.Drawing.Size(128, 24);
            this._protectTabStripMenuItem.Text = "Protect TabStrip";
            this._protectTabStripMenuItem.Click += new System.EventHandler(this.OnProtectTabStripMenuItemClick);
            // 
            // _formatCellsMenuItem
            // 
            this._formatCellsMenuItem.Name = "_formatCellsMenuItem";
            this._formatCellsMenuItem.Size = new System.Drawing.Size(105, 24);
            this._formatCellsMenuItem.Text = "Format Cells";
            this._formatCellsMenuItem.Click += new System.EventHandler(this.OnFormatCellsMenuItemClick);
            // 
            // splitContainer1
            // 
            this.splitContainer1.Dock = System.Windows.Forms.DockStyle.Fill;
            this.splitContainer1.IsSplitterFixed = true;
            this.splitContainer1.Location = new System.Drawing.Point(0, 28);
            this.splitContainer1.Name = "splitContainer1";
            this.splitContainer1.Orientation = System.Windows.Forms.Orientation.Horizontal;
            // 
            // splitContainer1.Panel1
            // 
            this.splitContainer1.Panel1.Controls.Add(this.splitContainer2);
            // 
            // splitContainer1.Panel2
            // 
            this.splitContainer1.Panel2.Controls.Add(this._fpSpread);
            this.splitContainer1.Size = new System.Drawing.Size(636, 571);
            this.splitContainer1.SplitterDistance = 25;
            this.splitContainer1.TabIndex = 1;
            // 
            // splitContainer2
            // 
            this.splitContainer2.Dock = System.Windows.Forms.DockStyle.Fill;
            this.splitContainer2.IsSplitterFixed = true;
            this.splitContainer2.Location = new System.Drawing.Point(0, 0);
            this.splitContainer2.Name = "splitContainer2";
            // 
            // splitContainer2.Panel1
            // 
            this.splitContainer2.Panel1.Controls.Add(this.nameBox1);
            // 
            // splitContainer2.Panel2
            // 
            this.splitContainer2.Panel2.Controls.Add(this.panel1);
            this.splitContainer2.Size = new System.Drawing.Size(636, 25);
            this.splitContainer2.SplitterDistance = 149;
            this.splitContainer2.TabIndex = 0;
            // 
            // nameBox1
            // 
            this.nameBox1.Dock = System.Windows.Forms.DockStyle.Fill;
            this.nameBox1.Location = new System.Drawing.Point(0, 0);
            this.nameBox1.MaxLength = 255;
            this.nameBox1.Name = "nameBox1";
            this.nameBox1.Size = new System.Drawing.Size(149, 24);
            this.nameBox1.TabIndex = 0;
            // Attach nameBox1 to _fpSpread
            this.nameBox1.Attach(this._fpSpread);
            // 
            // _fpSpread
            // 
            this._fpSpread.AccessibleDescription = "Book1, Sheet1, Row 0, Column 0";
            this._fpSpread.Dock = System.Windows.Forms.DockStyle.Fill;
            this._fpSpread.Font = new System.Drawing.Font("Calibri", 11F);
            this._fpSpread.Location = new System.Drawing.Point(0, 0);
            this._fpSpread.Name = "_fpSpread";
            this._fpSpread.Size = new System.Drawing.Size(636, 542);
            this._fpSpread.TabIndex = 0;
            // 
            // panel1
            // 
            this.panel1.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
            this.panel1.Controls.Add(this.formulaTextBox1);
            this.panel1.Dock = System.Windows.Forms.DockStyle.Fill;
            this.panel1.Location = new System.Drawing.Point(0, 0);
            this.panel1.Name = "panel1";
            this.panel1.Size = new System.Drawing.Size(483, 25);
            this.panel1.TabIndex = 0;
            // 
            // formulaTextBox1
            // 
            this.formulaTextBox1.AcceptsTab = true;
            this.formulaTextBox1.BorderStyle = System.Windows.Forms.BorderStyle.None;
            this.formulaTextBox1.Dock = System.Windows.Forms.DockStyle.Fill;
            this.formulaTextBox1.Location = new System.Drawing.Point(0, 0);
            this.formulaTextBox1.Margin = new System.Windows.Forms.Padding(0);
            this.formulaTextBox1.Name = "formulaTextBox1";
            this.formulaTextBox1.RightToLeft = System.Windows.Forms.RightToLeft.No;
            this.formulaTextBox1.ScrollBars = System.Windows.Forms.RichTextBoxScrollBars.None;
            this.formulaTextBox1.Size = new System.Drawing.Size(481, 23);
            this.formulaTextBox1.TabIndex = 0;
            // Attach formulaTextBox1 to _fpSpread
            this.formulaTextBox1.Attach(this._fpSpread);
            // 
            // ProtectionDialog
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 16F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.Name = "ProtectionDialog";
            this.pnlMain.ResumeLayout(false);
            this.pnlMain.PerformLayout();
            this.menuStrip1.ResumeLayout(false);
            this.menuStrip1.PerformLayout();
            this.splitContainer1.Panel1.ResumeLayout(false);
            this.splitContainer1.Panel2.ResumeLayout(false);
            ((System.ComponentModel.ISupportInitialize)(this.splitContainer1)).EndInit();
            this.splitContainer1.ResumeLayout(false);
            this.splitContainer2.Panel1.ResumeLayout(false);
            this.splitContainer2.Panel2.ResumeLayout(false);
            ((System.ComponentModel.ISupportInitialize)(this.splitContainer2)).EndInit();
            this.splitContainer2.ResumeLayout(false);
            ((System.ComponentModel.ISupportInitialize)(this._fpSpread)).EndInit();
            this.panel1.ResumeLayout(false);
            this.ResumeLayout(false);

        }

        #endregion

        private System.Windows.Forms.ContextMenuStrip contextMenuStrip1;
        private System.Windows.Forms.MenuStrip menuStrip1;
        private System.Windows.Forms.ToolStripMenuItem _protectSheetMenuItem;
        private System.Windows.Forms.SplitContainer splitContainer1;
        private System.Windows.Forms.SplitContainer splitContainer2;
        private FarPoint.Win.Spread.NameBox nameBox1;
        private System.Windows.Forms.Panel panel1;
        private FarPoint.Win.Spread.FormulaTextBox formulaTextBox1;
        private FarPoint.Win.Spread.FpSpread _fpSpread;
        private FarPoint.Win.Spread.SheetView _fpSpread_Sheet1;
        private System.Windows.Forms.ToolStripMenuItem _protectTabStripMenuItem;
        private System.Windows.Forms.ToolStripMenuItem _formatCellsMenuItem;
    }
}
